package com.zhongge.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhongge.entity.Clazz;
import com.zhongge.service.impl.ClassServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhongge
 * @since 2025-11-09
 */
@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassServiceImpl classService;
    /**
     * post 请求 添加班级
     * @return 结果
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addClass(@RequestBody Clazz clazz){
        Map<String, Object> map = new HashMap<>();
        try{
            boolean save = classService.save(clazz);
            map.put("success", true);
            map.put("msg", "插入成功");
        }catch (Exception e){
            map.put("success", false);
            map.put("msg", "插入失败"+e.getMessage());
        }
        return map;
    }

    /**
     * post-请求
     * 根据班级id删除班级
     * @param id 班级编号
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteClass(Integer id){
        Map<String, Object> map = new HashMap<>();
        try{
            boolean save = classService.removeById(id);
            map.put("success", true);
            map.put("msg", "删除成功");
        }catch (Exception e){
            map.put("success", false);
            map.put("msg", "删除失败"+e.getMessage());
        }
        return map;
    }

    /**
     * 根据班级ID 修改班级信息
     * @param clazz 含有班级ID
     * @return 修改结果
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> updateClass(@RequestBody Clazz clazz){
        Map<String, Object> map = new HashMap<>();
        try{
            boolean save = classService.updateById(clazz);
            map.put("success", true);
            map.put("msg", "修改成功");
        }catch (Exception e){
            map.put("success", false);
            map.put("msg", "修改失败"+e.getMessage());
        }
        return map;
    }

    /**
     * 根据班级名称查询班级id
     * @param className
     * @return
     */
    @PostMapping("/getClassByName")
    @ResponseBody
    public Map<String, Object> getClassByName(String className){
        Map<String, Object> map = new HashMap<>();
        try{
            QueryWrapper<Clazz> clazzQueryWrapper = new QueryWrapper<>();
            clazzQueryWrapper.eq("name",className);
            Clazz one = classService.getOne(clazzQueryWrapper);

            map.put("success", true);
            map.put("msg", one.getId());
        }catch (Exception e){
            map.put("success", false);
            map.put("msg", "没有对应班级"+e.getMessage());
        }
        return map;
    }

    //根据教师ID 查询与该教师关联的所有班级信息 班级id 班级名称+对应的年级
    @GetMapping("/getTreeData")
    @ResponseBody
    public List<Map<String, Object>> getTreeData(@RequestParam(required = true) Integer id){
        //
        return classService.getClazzInfoByTeacherID(id);
    }
}
